package com.zlm.user.emp.controller;

import com.zlm.common.base.BaseController;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.dto.QqResponseDto;
import com.zlm.common.dto.WechatResultDto;
import com.zlm.common.dto.userEmp.SysEmpDto;
import com.zlm.user.emp.entity.SysEmpDO;
import com.zlm.user.emp.entity.dto.LoginAppParamsDto;
import com.zlm.user.emp.service.LoginService;
import com.zlm.user.emp.service.SysEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 登陆controller-(内部服务)
 * @Author: duyufu
 * @Date: 2019/4/22 9:16
 */
@RestController
@RequestMapping("/login")
public class LoginController extends BaseController {

    @Autowired
    private SysEmpService sysEmpService;

    @Autowired
    private LoginService loginService;

    /**
     * 用户登录
     * @param mobile
     * @return
     */
    @GetMapping(value = "/loginEmp", produces = "application/json")
    public JsonResultDto loginEmp(String mobile){
        try {
            SysEmpDto login = loginService.loginEmp(mobile);
            return success(login);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 用户注册
     * @param mobile
     * @param password
     * @return
     */
    @GetMapping(value = "/registerEmp")
    public JsonResultDto registerEmp(String mobile, String password) {
        try {
            SysEmpDto login = loginService.registerEmp(mobile, password);
            return success(login);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 环信注册
     * @param userId
     * @return
     */
    @GetMapping(value = "/huanxinRegister")
    public JsonResultDto huanxinRegister(Integer userId) {
        try {
            loginService.huanxinRegister(userId);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 强制下线
     * @param userHxId
     * @return
     */
    @GetMapping(value = "/huanxinDisconnect")
    public JsonResultDto huanxinDisconnect(String userHxId) {
        try {
            loginService.huanxinDisconnect(userHxId);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 验证码注册登录
     * @param mobile
     * @return
     */
    @GetMapping(value = "/activationLogin")
    public JsonResultDto activationLogin(String mobile) {
        try {
            SysEmpDto login = loginService.activationLogin(mobile);
            return success(login);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }


    /**
     * 微信用户注册
     * @param userInfo
     * @return
     */
    @PostMapping(value = "/wechatRegisterEmp")
    public JsonResultDto wechatRegisterEmp(@RequestBody WechatResultDto userInfo) {
        try {
            Integer empId = sysEmpService.wechatRegisterEmp(userInfo);
            return success(empId);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * QQ用户注册
     * @param userInfo
     * @return
     */
    @PostMapping(value = "/qqRegisterEmp")
    public JsonResultDto qqRegisterEmp(@RequestBody QqResponseDto userInfo) {
        try {
            Integer empId = sysEmpService.qqRegisterEmp(userInfo);
            return success(empId);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 腾讯登录
     * @param emp
     * @return
     */
    @PostMapping(value = "/tencentLogin", produces = "application/json")
    public JsonResultDto tencentLogin(@RequestBody SysEmpDO emp) {
        try {
            Map<String, Object> map = loginService.tencentLogin(emp);
            return success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 绑定手机号
     * @param loginAppParamsDto
     * @return
     */
    @PostMapping(value = "/bindMobile", produces = "application/json")
    public JsonResultDto bindMobile(@RequestBody LoginAppParamsDto loginAppParamsDto) {
        try {
            Map<String, Object> map = loginService.bindMobile(loginAppParamsDto);
            return success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

}
